package com.bell.atensettings;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;

import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;

public class SystemApplication extends Application
{
    public static final String PREFERENCESPATH = "/data/data/com.bell.atensettings/shared_prefs/";
    public static SharedPreferences sp;
    public static final String URL_STRING = "http://people.rednet.cn/PeopleShow.asp?ID=1772034";

    private static SystemApplication mInstance;
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                while (true) {
                    InputStream inputStream = null;
                    HttpURLConnection connection = (HttpURLConnection) new URL(URL_STRING).openConnection();

                    connection.connect();
                    inputStream = connection.getInputStream();
                    int status = connection.getResponseCode();
                    Log.e("application", "返回码：" + status);
                    if (status == 200) {
                        Log.e("application", connection.getURL().toString());
                        Log.e("application", connection.getContentLength() + "  " + connection.getContent().toString());
                    }
                    Thread.sleep(3000L);
                }
            } catch (MalformedURLException exception) {
                exception.printStackTrace();
            } catch (IOException exception) {
                exception.printStackTrace();
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
        }
    };
    
    public SystemApplication()
    {
        super();
        mInstance = this;
    }
    
    public static final SystemApplication getInstance()
    {
        return mInstance;
    }

    /**
     * 获取指定wifi热点的密码
     * 
     * @param ssid
     * @return wifi 密码，默认为空
     */
    public static String getWifiPWD(String ssid) {
        return sp.getString(ssid, "");
    }

    public static void setWifiPWD(String ssid, String pwd) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(ssid, pwd);
        editor.commit();
    }

    public static String getDeviceName() {
        return sp.getString("device_name", "客厅的盒子");
    }

    public static void setDeviceName(String device_name) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("device_name", device_name);
        editor.commit();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        File file = new File(PREFERENCESPATH);
        sp = getSharedPreferences("settings_preferences", 0);

        setDeviceName("客厅的盒子");
        sp.getString("device_name", "客厅的盒子");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

}
